﻿using System.ComponentModel;
using System.IO;
using System.Linq;
using FileSynchronizer.DataAccess;
using FileSynchronizer.Forms;
using mylibrary;

namespace FileSynchronizer.Objects
{
    //---------------------------------------------------------------------------------------------------------------------------------------
    // Code based upon the State Pattern, as described by the GOF
    //
    // http://en.wikipedia.org/wiki/State_pattern and http://en.wikipedia.org/wiki/Talk:State_pattern#Disputed
    //
    // This is a Concrete State Object
    //
    //---------------------------------------------------------------------------------------------------------------------------------------

    class ProgramState_Enterprise_SyncAllFolderpairs : ProgramState
    {
        #region Private variables
        //---------------------------------------------------------------------------------------------------------------------------------------
        private RunWorkerCompletedEventHandler myExternalCompletionHandler;
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------
        public ProgramState_Enterprise_SyncAllFolderpairs(ProgramContext TheProgramContext)
            : base(TheProgramContext)
        {
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public Methods
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when we want to Synchronize all FolderPairs
        //---------------------------------------------------------------------------------------------------------------------------------------
        public override bool SynchronizeAllFolderPairs(IQueryable<FolderPair> TheFolderPairs, RunWorkerCompletedEventHandler ExternalCompletionHandler)
        {
            Form_Progress frm_SyncProgress;
            bool OperationResult = true;
            bool LatencyOK = true;

            myExternalCompletionHandler = ExternalCompletionHandler;

            foreach (FolderPair myFolderPair in TheFolderPairs)
            {
                if (Directory.Exists(myFolderPair.LeftFolder) & Directory.Exists(myFolderPair.RightFolder))
                {
                    if (CurrentProgramContext.Settings.LatencyEnabled)
                    {
                        if (CurrentProgramContext.Settings.LatencyTimeout <= (NetworkDriveHelper.PathLatency(myFolderPair.LeftFolder) + NetworkDriveHelper.PathLatency(myFolderPair.RightFolder)))
                        {
                            LatencyOK = false;
                            // Log it?
                        }
                    }
                    if (LatencyOK)
                    {
                        frm_SyncProgress = new Form_Progress(myFolderPair);

                        frm_SyncProgress.ShowDialog();
                        if (!frm_SyncProgress.SyncSuccessfull)
                        {
                            Program.TheErrorForm.AddErrorString("Failed to synchronize " + myFolderPair.LeftFolder + " with " + myFolderPair.RightFolder);
                            OperationResult = false;
                        }
                        frm_SyncProgress = null;
                    }
                }
            }

            ChangeState(new ProgramState_Idle(CurrentProgramContext));
            CurrentProgramContext.CurrentState = State.Idle;

            if (myExternalCompletionHandler != null)
            {
                myExternalCompletionHandler(null, null);
            }

            return OperationResult;
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

    }
}
